package com.milks.filter;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.milks.dao.ProductDao;
import com.milks.dao.impl.ProductDaoImpl;
import com.milks.entity.Product;
import com.milks.service.HtmlService;
import com.milks.service.ProductService;
import com.milks.service.impl.HtmlServiceImpl;
import com.milks.service.impl.ProductServiceImpl;

public class HtmlFilter implements Filter {
	
	//@Resource
	private HtmlService htmlService;
	//@Resource
	private ProductService productService;
	//@Resource
	private ProductDao productDao;
	
	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
		
		HttpServletRequest req = (HttpServletRequest)request;
		HttpServletResponse res = (HttpServletResponse) response;
		res.setCharacterEncoding("utf-8");
		//   /51milks
		String contextPathString = req.getContextPath();
		//  /51milks/html/article_content/201411/90feaa077d124f239670ddf864cb7626.html
		String currentURL = req.getRequestURI();
		
		if (currentURL.contains("product_content")) {
			
		}
		// 得到访问文件名. 例如 /90feaa077d124f239670ddf864cb7626.html
		String targetURL = currentURL.substring(currentURL.lastIndexOf("/"),
				currentURL.length());
		// 得到htmlFilePath:/html/article_content/201411/90feaa077d124f239670ddf864cb7626.html
		String htmlFilePath = currentURL.split(contextPathString)[1];
		// 对文件名进行拆分. 先得到/index 然后在得到index /90feaa077d124f239670ddf864cb7626
		String splitName = targetURL.split("\\.")[0];
		// 90feaa077d124f239670ddf864cb7626
		String name = splitName.split("/")[1];
		String fileName = name + ".html";
		///51milks/admin/product/productUpdate
		// 判断文件是否存在. 存在的话则执行跳转. 否则执行生成
		//String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort();
		//String basePath = "http:////////localhost:8080";
		String savePath = request.getServletContext().getRealPath("/");
		File file = new File(savePath+htmlFilePath);
		if (!file.exists()) {
			System.out.println("生成HTML文件,执行跳转..");
			Product product2 = productDao.getProductByHtmlFilePath(htmlFilePath);
			htmlService.productContentBuildHtml(product2);
			//res.setHeader("REFRESH", "0;URL=" + currentURL);
			System.out.println("结束生成HTML文件,执行跳转..");
		} else {
			System.out.println("目标文件已经存在,执行跳转...");
		}
		chain.doFilter(req, res);
		
	}

	@Override
	public void init(FilterConfig config) throws ServletException {
		ServletContext context = config.getServletContext();  
        ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);  
        productService = (ProductService) ctx.getBean("productServiceImpl");
        htmlService = (HtmlService) ctx.getBean("htmlServiceImpl");
        productDao = (ProductDaoImpl) ctx.getBean("productDaoImpl");
	}

}
